package com.justrun.zjutsystem.zjutsystem.controller;

import com.justrun.response.ResultEntity;
import com.justrun.zjutsystem.zjutsystem.entity.SysMenu;
import com.justrun.zjutsystem.zjutsystem.entity.SysRoleMenu;
import com.justrun.zjutsystem.zjutsystem.service.SysMenuService;
import com.justrun.zjutsystem.zjutsystem.service.SysRoleMenuService;
import com.justrun.zjutsystem.zjutsystem.service.SysRoleService;
import com.justrun.zjutsystem.zjutsystem.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员角色权限管理模块
 * @author Chandler
 * @since 2020-10-09
 */
@Api("角色权限管理")
@RestController
@RequestMapping("/admin")
//@RequiresRoles("ADMIN")
public class AdminController {

    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysUserService userService;
    @Autowired
    SysRoleMenuService roleMenuService;

    @ApiOperation("获取权限菜单列表")
    @GetMapping("/getMenus")
    public ResultEntity getMenuList(){
        return ResultEntity.ok().data(menuService.list());
    }

    @ApiOperation("获取角色信息列表")
    @GetMapping("/getRoles")
    public ResultEntity getRoleList(){
        return ResultEntity.ok().data(roleService.list());
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/getUsers")
    public ResultEntity getUserList(){
        return ResultEntity.ok().data(userService.list());
    }

    @ApiOperation("根据用户ID查询角色")
    @GetMapping("/getRolesByUserId")
    public ResultEntity getRolesByUserId(@RequestParam Integer userId){
        return ResultEntity.ok().data(roleService.selectSysRoleByUserId(userId));
    }

    @ApiOperation("根据角色ID查询权限菜单")
    @GetMapping("/getMenusByRoleId")
    public ResultEntity getMenusByRoleId(@RequestParam Integer roleId){
        return ResultEntity.ok().data(menuService.selectSysMenuByRoleId(roleId));
    }

    @ApiOperation("添加权限")
    @PostMapping("/addMenu")
    public ResultEntity addMenu(@RequestParam String name,@RequestParam String perms){
        SysMenu menu = new SysMenu();
        menu.setName(name);
        menu.setPerms(perms);
        if (menuService.save(menu)){
            return ResultEntity.ok();
        }else {
            return ResultEntity.fail();
        }
    }

    @ApiOperation("添加管理员权限")
    @PostMapping("/addAdminMenu")
    public ResultEntity addAdminMenu(@RequestParam Integer menuId){

        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(1);
        sysRoleMenu.setMenuId(menuId);
        if (roleMenuService.save(sysRoleMenu)){
            return ResultEntity.ok();
        }else {
            return ResultEntity.fail();
        }
    }

    @ApiOperation("添加教师权限")
    @PostMapping("/addTeacherMenu")
    public ResultEntity addTeacherMenu(@RequestParam Integer menuId){

        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(2);
        sysRoleMenu.setMenuId(menuId);
        if (roleMenuService.save(sysRoleMenu)){
            return ResultEntity.ok();
        }else {
            return ResultEntity.fail();
        }
    }
}

